package org.flashbrighton.as3bots.views
{	
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.flashbrighton.as3bots.bots.BaseBot;
	import org.flashbrighton.as3bots.events.DeadBotEvent;
	import org.flashbrighton.as3bots.models.*;
	import org.flashbrighton.as3bots.views.ui.Names;
	import org.flashbrighton.as3bots.views.ui.ScoreBoard;

	public class GameView extends Sprite
	{
		private var _model:GameModel;
		private var _arena:ArenaView;
		private var _names:Names;
		
		public function GameView(model:GameModel)
		{
			_model = model;			
			_model.addEventListener(DeadBotEvent.DEAD, removeBot);	
		}
		
		
		public function addViews():void
		{
			var bot:BaseBot;			
			var skin:Sprite;
			
			_arena = new ArenaView();
			addChild(_arena);
			
			// Bots			
			for each (bot in _model.botList)
			{
				skin = bot.chassis.skin;
				skin.getChildAt(0).width = bot.chassis.radius * 2;
				skin.getChildAt(0).height = bot.chassis.radius * 2;								
				_arena.addChild(skin);
			}
			
		}
		
		
		public function update(e:Event=null):void
		{
			var bot:BaseBot;
			var skin:Sprite;
			
			// Go through body list and update sprite positions/rotations			
			for each (bot in _model.botList)
			{
				skin = bot.chassis.skin;
				skin.x = bot.model.body.x;
				skin.y = bot.model.body.y;
				skin.rotation = bot.model.body.r * (180/Math.PI);
			}
			
			//_model.physics.drawWireframes(graphics);
		}
		
		
		public function setUI():void
		{
			addChild(new ScoreBoard(_model));
			
			_names = new Names(_model);
			addChild(_names);
			
			//addChild(new Stats());
		}
		
		
		public function removeBot(e:DeadBotEvent):void
		{
			_arena.removeChild(_model.botViews[e.bot]);
		}
		
		
		public function get names():Names
		{
			return _names;
		}		
		
	}
}